package com.jz.community.moduleshoppingguide.nearshop.ui.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.bean.baseCartInfo.NewAddCartInfo;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.task.AddCartTask;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.basecomm.utils.rxbus.RxBus;
import com.jz.community.basecomm.utils.rxbus.eventType.ToCartRefreshEvent;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.nearshop.bean.CartGoodInfo;
import com.jz.community.moduleshoppingguide.nearshop.model.NearShopModel;
import com.jz.community.moduleshoppingguide.nearshop.model.NearShopModelImp;
import com.jz.community.moduleshoppingguide.nearshop.ui.view.AddWidget;

import java.util.List;

public class SelectedGoodsAdapter extends BaseQuickAdapter<CartGoodInfo, BaseViewHolder> {


    private String shopId;
    IChangListener changListener;

    public SelectedGoodsAdapter(int layoutResId, @Nullable List<CartGoodInfo> data, String shopId) {
        super(layoutResId, data);
        this.shopId = shopId;
    }

    @Override
    protected void convert(BaseViewHolder helper, CartGoodInfo item) {
        TextView goods_title = helper.getView(R.id.tv_selected_goods_title);
        TextView goods_price = helper.getView(R.id.selected_goods_price);
        TextView goods_standard = helper.getView(R.id.selected_goods_standard);
        AddWidget goods_view_add = helper.getView(R.id.selected_goods_view_add);
        goods_title.setText(item.getGoodsName());
        if (!TextUtils.isEmpty(item.getDiscountPrice())) {
            goods_price.setText("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(item.getDiscountPrice())));
        } else {
            goods_price.setText("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(item.getPrice())));
        }
        goods_standard.setText(item.getSkuName());
        goods_view_add.setCount(item.getNum());
        goods_view_add.setAddClick(new AddWidget.OnAddClick() {
            @Override
            public void onAddClick(View view, AddWidget addWidget, int count) {
                new AddCartTask((Activity) mContext, o -> {
                    NewAddCartInfo newAddCartInfo = (NewAddCartInfo) o;
                    if (Preconditions.isNullOrEmpty(newAddCartInfo)) {
                        WpToast.l(mContext, "加入购物车失败");
                        addWidget.resetCount(1);
                    } else {
                        item.setNum(item.getNum() + 1);
                        notifyDataSetChanged();
                        changListener.onChange(item, 1);
                    }
                }).execute("2", item.getGsId() + "", "1", shopId, item.getSkuId() + "", "");
            }

            @Override
            public void onSubClick(AddWidget addWidget, int count) {
                if (item.getNum() > 1) {
                    new AddCartTask((Activity) mContext, o -> {
                        NewAddCartInfo newAddCartInfo = (NewAddCartInfo) o;
                        if (Preconditions.isNullOrEmpty(newAddCartInfo)) {
                            WpToast.l(mContext, "加入购物车失败");
                            addWidget.resetCount(1);
                        } else {
                            item.setNum(item.getNum() - 1);
                            notifyDataSetChanged();
                            changListener.onChange(item, 0);
                        }
                    }).execute("2", item.getGsId() + "", "-1", shopId, item.getSkuId() + "", "");
                } else {
                    NearShopModel nearShopModel = new NearShopModelImp(mContext);
                    nearShopModel.deleteCartGoodsInfo(new Long[]{Long.parseLong(item.getId() + "")}, new OnLoadListener<BaseResponseInfo>() {
                        @Override
                        public void onSuccess(BaseResponseInfo baseResponseInfo) {
                            changListener.onChange(item, -1);
                            mData.remove(item);
                            notifyItemRemoved(helper.getAdapterPosition());
                            notifyItemRangeChanged(helper.getAdapterPosition(), mData.size() - helper.getAdapterPosition());
                            if (mData.size() == 0) {
                                changListener.isClear();
                            }
                            ToCartRefreshEvent toCartRefreshEvent = new ToCartRefreshEvent();
                            toCartRefreshEvent.setShopCartRefresh(true);
                            RxBus.getInstance().post(toCartRefreshEvent);
                        }

                        @Override
                        public void onFail(String s, int code) {
                            WpToast.l(mContext, s);
                            addWidget.resetCount(0);
                        }
                    });
                }
            }

            @Override
            public void onNumClick() {

            }
        });
    }

    public void setChangListener(IChangListener changListener) {
        this.changListener = changListener;
    }

    public interface IChangListener {
        void onChange(CartGoodInfo item, int type);
        void isClear();
    }
}
